<template>
  <div class="app-container">
    <el-form :model="queryParams2" ref="queryForm2" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item v-if="hisVersionVal" label="版本号" prop="hisVersionVal">
        <el-input disabled="true"  style="width: 300px"
                  v-model="queryParams2.hisVersionVal"
                  placeholder="请输入版本号"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    </el-form>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="需求BUG编码" prop="demandBugId">
        <el-input
          v-model="queryParams.demandBugId"
          placeholder="请输入需求BUG编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reportworld:datademandbug:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reportworld:datademandbug:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reportworld:datademandbug:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border="border" max-height="550px" v-loading="loading" :data="datademandbugList" @selection-change="handleSelectionChange"
              @sort-change="handleSortChange" :row-style="tableRowStyle"  @row-click="rowClick"   ref="datademandbug">
      <el-table-column type="selection" width="55" align="center" />
   <!--   <el-table-column v-hasPermi="['reportworld:datademandbug:remove']"
        label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['reportworld:datademandbug:remove']"
          >删除</el-button>
        </template>
      </el-table-column>-->
      <el-table-column label="需求BUG日期" align="center" prop="demandBugDate" width="180" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.demandBugDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需求内容" width="200" align="center" prop="description" :show-overflow-tooltip="true"/>
      <el-table-column label="需求BUG编号" width="180" align="center" prop="demandBugNum" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="需求/BUG" width="150" align="center"
                       :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">{{ scope.row.demandBugType === 1 ? '需求' : 'BUG' }}</template>
      </el-table-column>
      <el-table-column label="外部/内部" width="150" align="center"
                       :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">{{ scope.row.demandBugFsource === 1 ? '外部' : '内部' }}</template>
      </el-table-column>
      <el-table-column label="版本号" width="180" align="center" prop="version" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
     </el-table>

   <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />-->

    <!-- 添加或修改数据和需求BUG关联关系对话框 -->
    <el-dialog :title="title" :close-on-click-modal ="false" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="需求BUG编号" prop="demandBugId">
              <el-select filterable clearable v-model="form.demandBugId" style="width: 90%" placeholder="请选择需求BUG编号">
                <el-option
                  v-for="demandBugInfo in demandBugInfoList"
                  :key="demandBugInfo.id"
                  :label="`${demandBugInfo.demandBugNum}【${demandBugInfo.description.slice(0,50)}】`"
                  :value="demandBugInfo.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="butloading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listDatademandbug, getDatademandbug, delDatademandbug, addDatademandbug, updateDatademandbug,init } from "@/api/reportworld/datademandbug";
  import { getTableRowStyle } from "@/utils/yss";
  export default {
    props :{
      dataId: {type: String,default: ""},
      dataCode: { type: String,default: ""},
      hisVersion: { type: String,default: ""}
    },
    name: "Datademandbug",
    dicts: ['sys_common_status', 'sys_yes_no', 'report_version'],
    data() {
      return {
        butloading:false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 数据和需求BUG关联关系表格数据
        datademandbugList: [],
        // 需求BUG
        demandBugInfoList:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        queryParams2:{},
        hisVersionVal:false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 1000,
          dataId: null,
          demandBugId: null,
          dataCode: null,
          fileName: null,
          hisVersion:null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          demandBugId: [
            { required: true, message: "需求BUG编号不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      if(this.dataId != undefined && this.dataId && this.dataCode != undefined && this.dataCode ){
        this.queryParams.dataCode = this.dataCode;
        this.queryParams.dataId = this.dataId;
        this.queryParams.hisVersion = this.hisVersion;
        this.hisVersionVal = false;
        if (this.queryParams.hisVersion) {
          this.hisVersionVal = true;
          this.queryParams2.hisVersionVal = this.queryParams.hisVersion;
        }
        this.init();
        this.getList();
      }
    },
    methods: {
      /** 查询数据和需求BUG关联关系列表 */
      getList() {
        this.loading = true;
        listDatademandbug(this.queryParams).then(response => {
        this.datademandbugList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      },
      init(){
        init(this.queryParams).then(response => {
        this.demandBugInfoList=response.demandBugInfoList;
      });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          dataId: null,
          demandBugId: null,
          dataCode: null,
          fileName: null,
          hisVersion:null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 排序触发事件 */
      handleSortChange(column, prop, order) {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
        this.getList();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.init();
        this.open = true;
        this.form.dataId =this.queryParams.dataId;
        this.form.dataCode =this.queryParams.dataCode;
        this.title = "添加数据和需求BUG关联关系";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.init();
        const id = row.id || this.ids
        this.queryParams.id=id[0];
        getDatademandbug(this.queryParams).then(response => {
          this.form = response.data;
        this.open = true;
        this.title = "修改数据和需求BUG关联关系";
      });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.butloading=true;
            this.form.hisVersion =this.hisVersion;
            if (this.form.id != null) {
              updateDatademandbug(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {this.butloading=false});
            } else {
              addDatademandbug(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {this.butloading=false});
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const hisVersion= this.queryParams.hisVersion;
        this.$modal.confirm('是否确认删除数据项？').then(function() {
          return delDatademandbug(ids,hisVersion);
        }).then(() => {
          this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('reportworld/datademandbug/export', {
          ...this.queryParams
      }, `datademandbug_${new Date().getTime()}.xlsx`)
      },
      tableRowStyle({ row, rowIndex}){
        return getTableRowStyle(row,rowIndex,this.ids);
      },
      rowClick(row, column, event) { //单击行
        this.$refs.datademandbug.toggleRowSelection(row);
      }
    }
  };
</script>
